/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.webmanager.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.group.DefaultGroupA;
import com.tbit.common.group.DefaultGroupB;
import com.tbit.common.group.DeleteGroup;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.dto.DetentionMachineBoundDTO;
import com.tbit.uqbike.object.pojo.vo.DetentionMachineBoundVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.DetentionMachineBoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 扣押车辆
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 15:55 2023/2/8
 */
@Validated
@RestController
@RequestMapping("/detentionMachine")
public class DetentionMachineBoundController {


    @Autowired
    private DetentionMachineBoundService detentionMachineBoundService;


    /**
     * 根据扣押区id查询扣押车辆
     */
    @RequestMapping("/selectByDetentionId")
    public JsonResult selectByDetentionId(Integer detentionId, Integer accountId) {
        List<DetentionMachineBoundVO> stockMachineVOS = detentionMachineBoundService.selectByDetentionId(detentionId, accountId);
        return JsonResult.ok(stockMachineVOS);
    }

    /**
     * 查询扣押车辆分页
     *
     * @param detentionId 扣押区id
     * @param accountId   区域id
     * @param userCode    车辆编号
     */
    @GetMapping("/queryPage")
    public JsonResult queryPage(@NotNull Integer detentionId, @NotNull Integer accountId, String userCode,
                                @NotNull Integer pageNo, @NotNull Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<DetentionMachineBoundVO> detentionMachineList = detentionMachineBoundService.queryPage(detentionId, accountId, userCode);
        PageInfo<DetentionMachineBoundVO> pageInfo = new PageInfo(detentionMachineList);
        return JsonResult.ok(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 根据扣押区id和区域id移除扣押车辆
     *
     * @param dto .
     */
    @PostMapping("/deleteByDetentionIdAndMachineId")
    public Result<String> deleteByDetentionIdAndMachineId(@RequestBody @Validated(DeleteGroup.class) DetentionMachineBoundDTO dto) {
        detentionMachineBoundService.deleteByDetentionIdAndMachineId(dto.getAccountId(), dto.getDetentionId(), dto.getMachineId());
        return Result.success();
    }

    /**
     * 根据扣押区id和区域id批量添加扣押车辆
     *
     * @param dto .
     */
    @PostMapping("/batchAddDetentionMachine")
    @OperateLog(module = ModuleEnum.DETENTIONAREA, methods = MethodEnum.DETENTIONAREA_BATCH_ADD)
    public Result<String> batchAddDetentionMachine(@RequestBody @Validated(DefaultGroupA.class) DetentionMachineBoundDTO dto,
                                                   HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        int successNum = detentionMachineBoundService.batchAddDetentionMachine(dto.getAccountId(), dto.getDetentionId(),
                dto.getUserCodeList(), dto.getAddTime(), login.getAccountUserId());
        return Result.success("成功添加车辆" + successNum + "个", null);
    }

    /**
     * 根据扣押区id和区域id批量删除扣押车辆
     *
     * @param dto .
     */
    @PostMapping("/batchDeleteDetentionMachine")
    @OperateLog(module = ModuleEnum.DETENTIONAREA, methods = MethodEnum.DETENTIONAREA_BATCH_DEL)
    public Result<String> batchDeleteDetentionMachine(@RequestBody @Validated(DefaultGroupB.class) DetentionMachineBoundDTO dto) {
        int successNum = detentionMachineBoundService.batchDeleteDetentionMachine(dto.getAccountId(), dto.getDetentionId(),
                dto.getUserCodeList());
        return Result.success("成功删除车辆" + successNum + "个", null);
    }

}
